package com.ylx.util;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;



@WebServlet("/CheckCodeUtile")
public class CheckCodeUtile extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(70, 35,4,50);

        //图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(response.getOutputStream());
        //输出code
        String code = lineCaptcha.getCode();
        System.out.println("产生的验证码："+code);
        //将生成的验证码保存到session中
        HttpSession session = request.getSession();
        session.setAttribute("code",code);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
